package cn.neusoft.controller;

import cn.neusoft.domain.Customer;
import cn.neusoft.query.CustomerQuery;
import cn.neusoft.service.CustomerService;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.util.UUIDUtils;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Date;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController // @Controller + @ResponseBody
@RequestMapping(value = "/customer",produces = "application/json;charset=utf-8")
//接口文档的注解
@Api(value = "客户模块的API",description="客户相关的CRUD功能")
public class CustomerController {
  @Autowired
  CustomerService customerService;

  @PostMapping   //   Post + /customer
  @ApiOperation(value = "添加客户信息" )
  public ResultBean add(@RequestBody Customer customer){
    //判断客户姓名是否为空  为空则抛出异常
    if(customer==null){
      throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
    }
    if(StringUtils.isEmpty(customer.getCustomer_name())){
      throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
    }
    //调用service层的添加方法
    String customer_id= UUIDUtils.getUUID();
    Date create_time=new Date(System.currentTimeMillis());
    Date update_time=new Date(System.currentTimeMillis());
    customer.setCustomer_id(customer_id);
    customer.setCreate_time(create_time);
    customer.setUpdate_time(update_time);
    customerService.add(customer);
    System.out.println(create_time);
    return ResultBean.success();
  }

  @PutMapping ("/update")//   Put + /customer
  @ApiOperation(value = "修改客户信息" )
  public ResultBean update(@RequestBody Customer customer){

    if(customer==null){
      throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
    }
    if(StringUtils.isEmpty(customer.getCustomer_name())){
      throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
    }
    if(StringUtils.isEmpty(customer.getCustomer_id())){
      throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
    }
    Date update_time=new Date(System.currentTimeMillis());
    customer.setUpdate_time(update_time);
    System.out.println(customer);
    customerService.update(customer);
    return ResultBean.success();
  }


  @PutMapping ("/fakedelete") //   Put + /customer/
  @ApiOperation(value = "伪删除客户" )
  public ResultBean fakedelete(@RequestBody Customer customer){
    if(customer.getCustomer_id()==null){
      throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
    }

    String a=customerService.findifOrder(customer.getCustomer_id());
    if(Objects.equals(a, null)) {
      customer.setDel_flag("2");
      customerService.fakedelete(customer);
      return ResultBean.success();
    }else{
      throw new AppException(AppExceptionCodeMsg.CUSTOMER_HAVE_ORDER);
    }
  }

  @PutMapping ("/cancelfakedelete") //   Put + /customer/
  @ApiOperation(value = "取消伪删除客户" )
  public ResultBean cancelfakedelete(@RequestBody Customer customer){
    if(customer.getCustomer_id()==null){
      throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
    }
      customer.setDel_flag("0");
      customerService.fakedelete(customer);
      return ResultBean.success();
  }


  @DeleteMapping("/{customer_id}")   // Delete + /stu/1
  @ApiOperation(value = "删除客户信息" )
  public ResultBean delete(@PathVariable("customer_id") String customer_id){
    if(customer_id==null){
      throw new AppException(AppExceptionCodeMsg.CUSTOMER_NOT_EXISTS);
    }
      customerService.delete(customer_id);
      return ResultBean.success();

  }

  @PostMapping("/page")  //  Post + /customer/page
  @ApiOperation(value = "分页查询用户信息" )
  public ResultBean findByPage(@RequestBody CustomerQuery customerQuery){
    PageBean<Customer> pageBean = customerService.findByPage(customerQuery);
    return ResultBean.success(pageBean);
  }

  @PostMapping("/delpage")  //  Post + /customer/page
  @ApiOperation(value = "分页查询伪删除用户信息" )
  public ResultBean finddelByPage(@RequestBody CustomerQuery customerQuery){
    PageBean<Customer> pageBean = customerService.finddelByPage(customerQuery);
    return ResultBean.success(pageBean);
  }

}
